< FrankJS />

How to Force Git to Overwrite Local Files On “Pull” (Actually Fetch)

Please, read through this first before making changes, as there are some important points to consider.

If you have any files that are not tracked by Git, these files will not be affected by what we are about to do. See bottom of post for a long-winded explanation*.


In your terminal, while in your git project folder, enter the following commands:

git fetch --all git reset --hard origin/master

If you are on some other branch, then replace your_branch below with your branch’s name.

git reset --hard origin/your_branch

Explanation:

'git fetch' downloads the latest from remote without trying to merge or rebase anything.

'git reset' resets the master branch to what you just fetched. --hard option changes all the files in your working tree to match the files in ‘origin/master'.


Important:

It is possible to maintain current local commits by creating a branch from ‘master' before resetting:

git checkout master git branch new-branch-to-save-current-commits git fetch --all git reset --hard origin/master

This causes all of the old commits to be kept in new-branch-to-save-current-commits. However, the uncommitted changes, even staged, will be lost. Make sure to stash and commit anything you need. It is also prudent to make an actual copy of your project folder to somewhere else as a backup to be safe when doing something like this.

*This will only overwrite your local files that exist in the remote repository as well. If you would like the other files that are in your local project folder, but that do not exist in the remote repository,  to remain then you have nothing else to do. If you want your local project folder to have the exact structure and content of the remote repository being pulled over, then delete the contents of your local project folder first. This ensures that only the remote repository project’s files are in your local project folder post-pull.

Frank J Santaguida, 2022